package leetcode.easy;

public class $14_LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0)
            return "";
        if (strs.length == 1)
            return strs[0];
        StringBuilder sb = new StringBuilder();
        boolean skipFlag = false;
        int index = 0;
        while (true){
            for (int i = 1;i < strs.length;i++){
                if (strs[0].length() <= index){
                    skipFlag = true;
                    break;
                }
                if (strs[i].length() > index){
                    if (strs[i].charAt(index) == strs[i - 1].charAt(index)){
                        continue;
                    } else {
                        skipFlag = true;
                        break;
                    }
                } else {
                    skipFlag = true;
                    break;
                }
            }
            if(skipFlag){
                break;
            } else {
                sb.append(strs[0].charAt(index));
            }
            index++;
        }
        return sb.toString();
    }
}
